<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class player extends CI_Controller {

	public function index()
	{
		redirect();
	}

	public function percent($num_amount, $num_total) 
	{
            @$count1 = $num_amount / $num_total;
            $count2 = $count1 * 100;
            $count = number_format($count2, 0);
            return $count;
    }

	public function cs()
	{		
		if($this->uri->segment(3) == '')
		{
			redirect();
		}
        //////////////////////////////////////////////////////
		/////////////////////// session //////////////////////
		//////////////////////////////////////////////////////

		$all_session = $this->session->all_userdata();
		$id = @$all_session['login'][0]['mem_id'];
		$data['member'] = $this->main_model->get_member($id);
		
		$id = $this->uri->segment(3);
		$data['profile'] = $this->pvdo_model->get_profile($id);

		if($data['profile'][0]['vdo_status'] != '0')
		{
			redirect();
		}

		$data['thumb'] = $this->pvdo_model->get_thumb($id);
		$data['category'] = $this->pvdo_model->get_category();
		$data['web_service'] = $this->main_model->get_web_service();
		$data['new_vdo'] = $this->player_model->new_vdo();
		$data['vdo'] = $this->player_model->get_vdo($data['profile'][0]['category_id']);

		$view = $data['profile'][0]['vdo_view'] + 1 ;

		$input = array(
			'vdo_view' => $view,
			'vdo_id' => $id
		);
		
		$this->player_model->view_vdo($input);

		//////////////////////////////////////////////////////
		///////////////// check_last_history /////////////////
		//////////////////////////////////////////////////////
		if(@$data['member'][0]['mem_id'] != null){
			
			$vdo_id = $data['profile'][0]['vdo_id'];
			$mem_id = $data['member'][0]['mem_id'];

			$data['last_history'] = $this->player_model->check_last_history($mem_id);

			if($vdo_id != @$data['last_history'][0]['vdo_id'])
			{
				$check_last_history = array(
					'vdo_id' => $vdo_id,
					'mem_id' => $mem_id
				);
				$this->player_model->add_history($check_last_history);
			} 		
		}

		//////////////////////////////////////////////////////
		///////////////////// get Rate ///////////////////////
		//////////////////////////////////////////////////////
		$vdo_id = $data['profile'][0]['vdo_id'];
			
        $data['like_count'] = $this->player_model->like($vdo_id);
        $data['dislike_count'] = $this->player_model->dislike($vdo_id);
        $data['rate_all_count'] = $data['dislike_count'] + $data['like_count']; 

        $data['rate_like_percent'] = $this->percent($data['like_count'], $data['rate_all_count']);
        $data['rate_dislike_percent'] = $this->percent($data['dislike_count'], $data['rate_all_count']);


        @$data['check_favorite'] = $this->player_model->check_favorite($data['member'][0]['mem_id'] , $data['profile'][0]['vdo_id']);

        @$data['check_like'] = $this->player_model->check_liked($mem_id ,$vdo_id);
		@$data['check_dislike'] = $this->player_model->check_disliked($mem_id ,$vdo_id);

		$data['get_report'] = $this->player_model->get_report($vdo_id);

		$this->load->view('player',$data);

	}

	public function rate()
	{
		$act = $this->input->post('act');
		$mem_id = $this->input->post('mem_id');
		$vdo_id = $this->input->post('vdo_id');

		$like_rate = $this->player_model->check_liked($mem_id ,$vdo_id);
		$dislike_rate = $this->player_model->check_disliked($mem_id ,$vdo_id);

		if($like_rate != null)
		{
			$like_id = $like_rate[0]['like_id'];
			$like_rate = $like_rate[0]['like_rate'];
		} else {
			$like_rate = null;
		}
	
		if($dislike_rate != null)
		{
			$dislike_id = $dislike_rate[0]['dislike_id'];
			$dislike_rate = $dislike_rate[0]['dislike_rate'];
		} else {
			$dislike_rate = null;
		}
		
		
		$liked = count($like_rate);
		$disliked = count($dislike_rate);

		////////////////////////////////////////////////////
		//////////////////// check_like ////////////////////
		////////////////////////////////////////////////////

		if($act == 'like')
		{
			$input = array(

				'mem_id' => $mem_id,
				'like_rate' => 1,
				'vdo_id' => $vdo_id

			);

			if($like_rate == null)
			{
				$this->player_model->add_like($input);

				$dislike = array(

							'mem_id' => $mem_id,
							'dislike_rate' => 0,
							'vdo_id' => $vdo_id
				);
				$this->player_model->add_dislike($dislike);


				//////////////////////////////////////////////////////
				//////////////////// uplike_like  ////////////////////
				//////////////////////////////////////////////////////
				$data['add_all_like'] = $this->player_model->like($vdo_id);
				$data['add_all_dislike'] = $this->player_model->dislike($vdo_id);
				$all_like = array(
					'vdo_id' => $vdo_id,
					'vdo_like' => $data['add_all_like'],
					'vdo_dislike' => $data['add_all_dislike']
				);
				$this->player_model->update_all_like($all_like);
				
			} else {

				if($like_rate == 0)
				{
					if($dislike_rate == null)
					{
						$this->player_model->add_like($input);

					} else { 
						$data['add_all_like'] = $this->player_model->like($vdo_id);
						$data['add_all_dislike'] = $this->player_model->dislike($vdo_id);
						//////////////////////////////////////////////////////
						//////////////////// uplike_like  ////////////////////
						//////////////////////////////////////////////////////

						echo 'like: ',$data['add_all_like'];
						echo 'dislike: ',$data['add_all_dislike'];

						if($data['add_all_dislike'] != 0)
						{							
							$all_like = array(
								'vdo_id' => $vdo_id,
								'vdo_like' => $data['add_all_like']+1,
								'vdo_dislike' => $data['add_all_dislike']-1
							);
							$this->player_model->update_all_like($all_like);

							echo '<pre>';
							print_r($all_like);
							echo '3';
						} else 	{

							$all_like = array(
								'vdo_id' => $vdo_id,
								'vdo_like' => $data['add_all_like'],
								'vdo_dislike' => $data['add_all_dislike']
							);
							$this->player_model->update_all_like($all_like);

							echo '<pre>';
							print_r($all_like);
							echo '4';
						}

						$this->player_model->update_like($input ,$like_id);

						//update dislike_rate = 0
						$dislike = array(

							'mem_id' => $mem_id,
							'dislike_rate' => 0,
							'vdo_id' => $vdo_id

						);
						$this->player_model->update_dislike($dislike ,$dislike_id);
						

					}
				} 
					
				
			} 

			

		} 
		//////////////////////////////////////////////////////
		//////////////////// check_dislike ///////////////////
		//////////////////////////////////////////////////////

		if($act == 'dislike')
		{
			$input = array(

				'mem_id' => $mem_id,
				'dislike_rate' => 1,
				'vdo_id' => $vdo_id

			);

			

			if($dislike_rate == null)
			{
				$this->player_model->add_dislike($input);

				$like = array(

							'mem_id' => $mem_id,
							'like_rate' => 0,
							'vdo_id' => $vdo_id
				);
				$this->player_model->add_like($like);

				//////////////////////////////////////////////////////
				//////////////////// uplike_like  ////////////////////
				//////////////////////////////////////////////////////
				$data['add_all_like'] = $this->player_model->like($vdo_id);
				$data['add_all_dislike'] = $this->player_model->dislike($vdo_id);
				$all_like = array(
					'vdo_id' => $vdo_id,
					'vdo_like' => $data['add_all_like'],
					'vdo_dislike' => $data['add_all_dislike']
				);
				$this->player_model->update_all_like($all_like);

			} else {

			

				if($dislike_rate == 0)
				// if($dislike_rate[0]['dislike_rate'] == 0)
				{
					
					if($like_rate == null)
					{
						$this->player_model->add_dislike($input);
						
					} else { //ike_rate != null
						
						$data['add_all_like'] = $this->player_model->like($vdo_id);
						$data['add_all_dislike'] = $this->player_model->dislike($vdo_id);
						
						//////////////////////////////////////////////////////
						//////////////////// uplike_like  ////////////////////
						//////////////////////////////////////////////////////

						if($data['add_all_like'] != 0)
						{							
							$all_like = array(
								'vdo_id' => $vdo_id,
								'vdo_like' => $data['add_all_like']-1,
								'vdo_dislike' => $data['add_all_dislike']+1
							);
							$this->player_model->update_all_like($all_like);

							echo '<pre>';
							print_r($all_like);
							echo '1';
						} else 	{

							$all_like = array(
								'vdo_id' => $vdo_id,
								'vdo_like' => $data['add_all_like'],
								'vdo_dislike' => $data['add_all_dislike']
							);
							$this->player_model->update_all_like($all_like);

							echo '<pre>';
							print_r($all_like);
							echo '2';
						}

						//update dislike_rate = 1
						$this->player_model->update_dislike($input ,$dislike_id);

						//update like_rate = 0
						$like = array(
							'like_id' => $like_id,
							'mem_id' => $mem_id,
							'like_rate' => 0,
							'vdo_id' => $vdo_id

						);
					
						$this->player_model->update_like($like ,$like_id);

					}
				} 

			} //else

			

		} //if $act == 'dislike'

	
		
	}


	
}